﻿package {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.events.MouseEvent;

	/**
	 * ...
	 * @author chancidal
	 */
	public class Divide extends Sprite {
		//
		public static var MODE3:Number = 3;
		public static var MODE4:Number = 4;
		public static var MODE5:Number = 5;
		public static var finish:int = 0;
		//
		private var m_url:String = "";
		private var m_loader:Loader = new Loader();
		private var m_pieceNum:int = 0;
		private var m_line:Sprite;
		//
		private var pieceWidth:Number;
		private var pieceHeight:Number;
		//
		public function Divide(url:String, divideMode:Number = 3) {
			m_line = new Sprite();
			addChild(m_line);
			m_pieceNum = divideMode;
			m_url = url;
			m_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderHandler);
			m_loader.load(new URLRequest(m_url));
			//
		}
		//////////////////////////////////////////////////加载完成处理
		private function loaderHandler(evt:Event):void {
			m_loader.removeEventListener(Event.COMPLETE, loaderHandler);
			//背景模糊完成图和方格
			addChild(m_loader);
			m_loader.alpha = .3;
			//分割图片
			var bmpData:BitmapData = new BitmapData(m_loader.width,m_loader.height);
			bmpData.draw(m_loader);
			pieceWidth = m_loader.width / m_pieceNum;
			pieceHeight = m_loader.height / m_pieceNum;
			var pieceBmpData:BitmapData;
			var pieceBmp:Bitmap;
			var pieceCont:Sprite;
			m_line.graphics.lineStyle(1);
			//方格
			for(var k:int = 0;k<m_pieceNum+1;k++){
				m_line.graphics.moveTo(m_loader.x ,m_loader.y + pieceHeight * k);
				m_line.graphics.lineTo(m_loader.x + m_loader.width,m_loader.y + pieceHeight* k);
				m_line.graphics.moveTo(m_loader.x + pieceWidth * k,m_loader.y);
				m_line.graphics.lineTo(m_loader.x + pieceWidth * k,m_loader.y + m_loader.height);
			}
			//
			for(var i:int=0;i<m_pieceNum;i++){
				for(var j:int=0;j<m_pieceNum;j++){
					pieceBmpData = new BitmapData(pieceWidth,pieceHeight);
					pieceBmpData.copyPixels(bmpData,new Rectangle(i*pieceWidth,j*pieceHeight,pieceWidth,pieceHeight),new Point());
					pieceBmp = new Bitmap(pieceBmpData);
					pieceCont = new Sprite();
					addChild(pieceCont);
					pieceCont.addChild(pieceBmp);
					pieceCont.x = m_loader.x + m_loader.width + Math.random() * (stage.stageWidth - m_loader.x - m_loader.width - pieceCont.width - 3);
					pieceCont.y = m_loader.y + Math.random() * (stage.stageHeight - m_loader.y - pieceCont.height - 200);
					pieceCont.name = String(i) + String(j);
					pieceCont.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown_pieceCont);
				}
			}
		}
		//////////////////////////////////////////////////拖动小图片函数
		private function mouseDown_pieceCont(evt:MouseEvent):void{
			evt.target.startDrag();
			evt.target.parent.addChildAt(evt.target,evt.target.parent.numChildren-1);
			evt.target.addEventListener(MouseEvent.MOUSE_UP,mouseUP_pieceCont);
		}
		//////////////////////////////////////////////////停止拖动图片函数
		private function mouseUP_pieceCont(evt:MouseEvent):void{
			evt.target.stopDrag();
			evt.target.removeEventListener(MouseEvent.MOUSE_UP,mouseUP_pieceCont);
			//吸附效果
			/*trace(evt.target.name.charAt(0));
			trace(evt.target.name.charAt(1));*/
			var correctX:Number = Number(evt.target.name.charAt(0)) * pieceWidth + m_loader.x;
			var correctY:Number = Number(evt.target.name.charAt(1)) * pieceHeight + m_loader.y;
			if(Math.abs(correctX - evt.target.x) < pieceWidth * .1 && Math.abs(correctY - evt.target.y) < pieceHeight * .1){
				evt.target.x = correctX;
				evt.target.y = correctY;
				evt.target.mouseEnabled = false;
				finish ++;
			}
		}
		//////////////////////////////////////////////////
	}
}